<?php
header('Content-type:text/html;charset=utf8');
/**
 * 商城首页控制器
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-2-23
*/

class IndexController extends Controller{
	/**
	 * 首页控制器
	 * @author xjh
	 */
	function actionindex(){
		$goods_model = Goods::model();
		$info = $goods_model -> getGoodsByGuess();
		$user_model = new User();
		$lg_name = Yii::app()->user->id;
		$info = $user_model -> getIdByName("'$lg_name'");
		
		//获得最新上市推荐广告
		$last_adver = $goods_model-> getLastAdver();
		
		//获得最新上市广告
		$last_adverList = $goods_model -> getLastAdverList();
		
		//获得横条广告
		$hor_adverlist = $goods_model->getHoradver();
		
		//获得品牌汇总广告
		$brand_adverlist = $goods_model -> getBrandAdverList();
		
		$rand_number = rand(0,4);
		$hor_adverlist = $hor_adverlist[$rand_number];
		
		
		//品牌封面图
		$brand_hot = $goods_model -> getHotBrandCover();
		
		//热门品牌展示
		$brand_info = $goods_model -> getHotBrand();
		
		//轮播图展示
		$turn_infos = $goods_model->getTurnUrl(5);
		
		//搜索标签
		$tag_info = $goods_model->showTag();
		
		//遍历n条搜索导航
		$nav_info = $goods_model->all(8);
		$nav_side = $goods_model->allSide();
		
		//猜你喜欢部分(最近浏览的商品)
		$record_model = new Record();
		$record = array();
		if(!empty(Yii::app()->user->id)) {
			$record = $record_model -> getRecord(Yii::app()->user->id);
		}
		
		
		$counts_nums = $this->actioncount();
		
		$this -> render('index',array('info'=>$info,'nav_info'=>$nav_info,'nav_side'=>$nav_side,'tag_info'=>$tag_info,'turn_infos'=>$turn_infos,'brand_info'=>$brand_info,'last_adver'=>$last_adver,'last_adverList'=>$last_adverList,'hor_adverlist'=>$hor_adverlist,'brand_adverlist'=>$brand_adverlist,'brand_hot'=>$brand_hot,'record'=>$record,'counts_nums'=>$counts_nums));
	}
	
	/**
	 * 浏览量统计
	 */
	function actioncount(){
		$counterFile = "./counter.dat";
		$counterData = file($counterFile);
		
		$sign = False;
		for ($i = 0; $i <= count($counterData)-1; $i++) {
			$varArray = explode("|",$counterData[$i]);
			if($varArray[0] == @$GLOBALS["REQUEST_URI"]);{
				$varArray[1]++;
				$counterData[$i] = implode("|",$varArray);
				$sign=True;break;
			}
		}
		$data = implode("",$counterData);
		$fp = fopen($counterFile,"w");
		fputs($fp,$data);
		if(!$sign){
			fputs($fp,$GLOBALS["REQUEST_URI"]."|"."1"."|");
		}
		if(!empty($varArray[1])) { 
			return $varArray[1];
		}else{
			return 1;
		}
		fclose($fp);
	}
	
	
}
	
?>